package handler

import (
	"net/http"

	"github.com/gorilla/mux"

	"bbs/pkg/repository/mysql"
)

func NewHandler() *mux.Router {
	r := mux.NewRouter()

	// user
	userAPI := r.PathPrefix("/api/user").Subrouter()
	{
		userHandler := initializeUserHandler(mysql.DB)
		// register user
		userAPI.HandleFunc("/register", userHandler.Register).Methods(http.MethodPost)

		// login & logout user
		userAPI.HandleFunc("/login", userHandler.Login).Methods(http.MethodPost)
		userAPI.HandleFunc("/logout", userHandler.Logout).Methods(http.MethodGet)

		// settings
		userAPI.HandleFunc("/settings", userHandler.GetSettings).Methods(http.MethodGet)
		userAPI.HandleFunc("/settings", userHandler.UpdateSettings).Methods(http.MethodPut)

		// reset password
		userAPI.HandleFunc("/reset", userHandler.ResetPassword).Methods(http.MethodPut)
		userAPI.HandleFunc("/email", userHandler.SendCaptcha).Methods(http.MethodPost)
	}

	return r
}
